var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
var p = require('../../../utils/password.js')
Page({

    data: {
        orderGoodsList: [],
        totalMoeny: 0,
        totalNums: 0,
        totalPoint: 0,
        usePoint: 0,
        defaultAddr: [],
        addressId: 0,
        note: "",
        img_url: c.img_url,
        user_point: 0,
        is_checked: true,
        password:'',
        is_show:false
    },
    //清除输入框
    clear_psw:function(){
        this.setData({
            password:''
        })
    },
    close_pwd:function(){
        this.setData({
            is_show: false
        })
    },
    onLoad: function(options) {
        utils.checkLogin();
        var goodsExt = JSON.parse(options.goodsExt);
        var totalMoeny = goodsExt.totalMoney;
        var totalPoint = 0;
        var usePoint = goodsExt.totalPoint;
        //默认使用积分抵扣
        if (wx.getStorageSync("userinfo").money_jf > goodsExt.totalPoint) { //如果用户积分大于抵扣积分
            totalMoeny = parseFloat(goodsExt.totalMoney) - parseFloat(goodsExt.totalPoint);
            totalPoint = goodsExt.totalPoint;
            usePoint = goodsExt.totalPoint;
        } else {
            totalMoeny = parseFloat(goodsExt.totalMoney) - parseFloat(wx.getStorageSync("userinfo").money_jf);
            totalPoint = wx.getStorageSync("userinfo").money_jf;
            usePoint = wx.getStorageSync("userinfo").money_jf;
        }
        this.setData({
            totalMoeny: totalMoeny.toFixed(2),
            totalPoint: totalPoint,
            usePoint: usePoint,
            totalNums: goodsExt.totalNums,
            orderGoodsList: JSON.parse(options.orderGoods),
            user_point: wx.getStorageSync("userinfo").money_jf
        });

    },
    onShow: function() {
        this.getDefaultAddr();
        // utils.updateUserinfo();
    },
    getDefaultAddr: function() { //获取默认地址
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url: "/wsgoods/Useraddress/getdefault",
            token: token,
        };
        utils.request(data).then(function(res) {
            if (res.code == 0) {
                var data = res.data;
                if (data.length == 0) {
                    that.setData({
                        defaultAddr: data,
                        addressId: 0
                    });
                } else {
                    that.setData({
                        defaultAddr: data,
                        addressId: data.id
                    });
                }
            } else {

            }
        });
    },
    editIsuseponit: function(e) {
        var checked = e.currentTarget.dataset.check;
        if (!checked) {
            if (this.data.user_point > this.data.usePoint) { //如果用户积分大于抵扣积分
                var totalMoeny = parseFloat(this.data.totalMoeny) - parseFloat(this.data.usePoint);
            } else {
                var totalMoeny = parseFloat(this.data.totalMoeny) - parseFloat(this.data.user_point);
            }
        } else {
            if (this.data.user_point > this.data.usePoint) { //如果用户积分大于抵扣积分
                var totalMoeny = parseFloat(this.data.totalMoeny) + parseFloat(this.data.usePoint);
            } else {
                var totalMoeny = parseFloat(this.data.totalMoeny) + parseFloat(this.data.user_point);
            }
        }
        this.setData({
            is_checked: !checked,
            totalMoeny: totalMoeny.toFixed(2),
            totalPoint: checked ? 0 : this.data.usePoint
        })
    },
    editNote: function(e) {
        this.setData({
            note: e.detail.value
        })
    },
    paypwd_btn: function() {
        this.setData({
            is_show:true
        })
    },
    bindKeyInput: function(e) {
        let value = e.detail.value
        let key = e.target.dataset.index;
        this.setData({
            password:value
        })
    },
    submit: function() { //提交创建订单
        console.log(this.data.password)
        if (this.data.addressId == 0) {
            utils.errMsg("请先选择一个地址");
            return false;
        }
        var token = wx.getStorageSync("userinfo").wap_token;
        var goods_ids = "";
        var data = this.data.orderGoodsList;
        data.forEach((item, index) => {
            goods_ids += item.id + "|" + item.nums + ",";
        })
        goods_ids = goods_ids.substr(0, goods_ids.length - 1);
        var userinfo = wx.getStorageSync("userinfo");
        var pay_money = this.data.totalMoeny - userinfo['money'];
        pay_money = pay_money > 0 ? pay_money : 0;
      //   var paypwd = this.data.password;
      //   if (!paypwd || paypwd < 100000) {
      //     utils.errMsg("请输入支付密码");
      //     return false;
      //   }
      //   if (paypwd < 100000){
      //     utils.errMsg("请输入支付密码");
      //     return false;
      //   }
      // if (paypwd.length != 6){
      //   utils.errMsg("支付密码必须为6位");
      //   return false;
      //   }
        var data = {
            url: "/wsgoods/ordergoods/add",
            token: token,
            goods_price_ids: goods_ids,
            openid: wx.getStorageSync("openId"),
            address_id: this.data.addressId,
            pay_money: pay_money,
            pay_type: 0,
            goods_type: this.data.goods_type,
            invitecode: wx.getStorageSync("invitecode") ? wx.getStorageSync("invitecode") : 0,
            note: this.data.note,
            totalPoint: this.data.totalPoint,
            isUsepoints: this.data.is_checked,
            spassword: ""
        };
        utils.request(data).then(function(res) {
            if (res.code == 0) {
                //直接支付成功!
                if (res.data.length == 0) {
                    utils.sucMsg("支付成功");
                    setTimeout(function() {
                      wx.redirectTo({
                            url: '/pages/shop/ordergoods/index',
                        })
                    }, 2000);
                    return false;
                }
                //走在线支付
                wx.requestPayment({
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr,
                    'package': res.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.paySign,
                    'success': function(res) {
                        utils.sucMsg("支付成功");
                        setTimeout(function() {
                          wx.redirectTo({
                                url: '/pages/shop/ordergoods/index',
                            })
                        }, 2000);
                    },
                    'fail': function(res) {
                        utils.errMsg("支付失败");
                        setTimeout(function() {
                          wx.redirectTo({
                                url: '/pages/shop/ordergoods/index',
                            })
                        }, 2000);
                    }
                })
            } else {
                utils.errMsg(res.msg);
            }
        });
    }
})